#include "MyWorkQueue.h"

MyWorkQueue::MyWorkQueue(bool startup)
	:mDefaultWorkQueue("Defautl")
{
	mDefaultWorkQueue.setWorkerThreadCount(2);
	mWorkQueueChannel = mDefaultWorkQueue.getChannel("Default");
	mDefaultWorkQueue.addResponseHandler(mWorkQueueChannel, this);
	mDefaultWorkQueue.addRequestHandler(mWorkQueueChannel, this);
	if (startup) 
	{
		mDefaultWorkQueue.startup();
	}
}

MyWorkQueue::~MyWorkQueue()
{
}

void MyWorkQueue::startup()
{
	mDefaultWorkQueue.startup();
}

void MyWorkQueue::processResponses()
{
	mDefaultWorkQueue.processResponses();
}

void MyWorkQueue::addRequest(TaskRequest request, int priority)
{
	mDefaultWorkQueue.addRequest(mWorkQueueChannel, request.type, Any(request), priority);
}

Response* MyWorkQueue::handleRequest(const Request* req, const WorkQueue* srcQ)
{
	if (req->getAborted())
	{
		return new Response(req, false, req->getData());
	}
	TaskRequest anyRequest = any_cast<TaskRequest>(req->getData());

	bool handle_succeed = anyRequest.type == RT_Type2;

	CORE_LOCK_AUTO_MUTEX;

		
	std::this_thread::sleep_for(std::chrono::milliseconds(1));

	return new Response(req, handle_succeed, Any(anyRequest));
}

void MyWorkQueue::handleResponse(const Response* res, const WorkQueue* srcQ)
{
	TaskRequest anyRequest = any_cast<TaskRequest>(res->getData());
	if (res->getRequest()->getAborted() || !res->succeeded())
	{
		return;
	}

	std::cout << std::this_thread::get_id() << " handle success " << anyRequest.message << "\n";
}
